'''
Created on 2019年1月4日

@author: allan
'''
from utils.http import httputils
from bs4 import BeautifulSoup
from utils.fake import mobile_idcard
from cpca import transform

class UserInfo():
    def __init__(self):
        self.name =None
        self.ID =None
        self.sex =None
        self.addr = None
        self.province =None
        self.city = None
        self.county = None
        self.phone = mobile_idcard.createPhone()

    
    def setNameID(self,str):
        tmp = str.split(' ')
        self.name = tmp[0]
        self.ID = tmp[1]

    def setAddress(self,str):
        df = transform([str])

        self.addr = str
        self.province= df.loc[0, '省']
        self.city= df.loc[0, '市']
        self.county= df.loc[0, '区']

    def __str__(self):
        return "%s,%s,%s,%s,%s,%s,%s,%s"% (self.phone,self.name,self.sex,self.ID,self.province,self.city,self.county,self.addr)
    
    def toDict(self):
        d ={}
        d["phone"]= self.phone
        d["name"]=self.name
        d["idcard"]=self.ID
        d["sex"]=self.sex
        d["address"]=self.addr
        d['province'] = self.province
        d['city'] = self.city
        d['county'] = self.county

        return d
    

def parserPage():
    ret = ''
    htmlsoup = httputils.getHtml2Soup('http://sfz.ckd.cc/idcard.php')
    
    for item in htmlsoup.find_all('tr') :
        if item.find('th'): continue
        
        u= UserInfo()
        
        for idx, tt in enumerate(item.find_all('td')):
            if idx==0:
                u.setNameID(tt.contents[0])
            elif idx==1:
                u.sex=tt.contents[0]
            else:
                u.setAddress(tt.contents[0].strip())

        ret+=str(u)+'\n'
        
    return ret


def main():
    print(parserPage())
            
            
    



if __name__ == '__main__':
     main()
    # df = transform(['河南省焦作市解放区']) #,'四川省凉山彝族自治州昭觉县','西藏自治区那曲地区安多县'])
    # # print(df.ix(0,3))
    # # print(df.iloc[0:2])
    # # print(df.ix[1:1, 3:3])
    # print(df.loc[0, '市'])